package com.wwwarehouse.contract.order_information;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.BuyOrderDetialBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.query_express.QueryExpressFragment;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/11/2 16:24
 * Description: 订单详情页面
 */

@Route(path = ContractConstant.PATH_BUYORDERDETAILSFRAGMENT)
public class BuyOrderDetailsFragment extends BaseFragment implements ElasticScrollView.OnPullListener, View.OnClickListener {
    private ImageView mImgPhoto;
    private TextView mNeedName, mType, mCount, mOrderTime, mSupplierName, mOrderNumber;
    private TextView mWeekPay, mOrderPay, mBankCardPay, mAliPay, mWechatPay, mOfflinePay;
    private TextView mTotalMoney, mAddress, mContant;
    private Button mBtnConfrim;
    private String contractUkid;
    private BuyOrderDetialBean mOrderDetailsBean;
    private StateLayout mStateLayout;
    private StateButton mBtnQuery;
    private ElasticScrollView mElasticScrollView;
    private boolean isShow = true;
    private int mOrderType;
    private View mSpaceView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_buy_order_details, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

    }

    private void initView() {
        mImgPhoto = $(R.id.detial_image);
        mNeedName = $(R.id.detial_need_name);
        mSupplierName = $(R.id.detial_supplier_name);
        mOrderNumber = $(R.id.detial_order_number);
        mType = $(R.id.detial_type);
        mCount = $(R.id.detial_count);
        mOrderTime = $(R.id.detial_order_time);
        mWeekPay = $(R.id.detial_weekpay);
        mOrderPay = $(R.id.detial_orderpay);
        mBankCardPay = $(R.id.detial_bankcardpay);
        mAliPay = $(R.id.detial_alipay);
        mWechatPay = $(R.id.detial_wechatpay);
        mOfflinePay = $(R.id.detial_offlinepay);
        mTotalMoney = $(R.id.detial_totalmoney);
        mAddress = $(R.id.detial_address);
        mContant = $(R.id.detial_contant);
        mBtnConfrim = $(R.id.btn_ordergoods);
        mStateLayout = $(R.id.state_layout);
        mBtnQuery = $(R.id.btn_query);
        mElasticScrollView = $(R.id.ecl_custom);
        mSpaceView = $(R.id.space_view);
        mElasticScrollView.setOnPullListener(this);

        if (getArguments() != null) {
            contractUkid = getArguments().getString("contractUkid");
            mOrderType = getArguments().getInt("orderType");
        }

        mStateLayout.showProgressView(true);
        mBtnConfrim.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BuyOrderDetailsFragment) {
            mActivity.setTitle("订单详情");
        }
    }

    @Override
    public void requestDatas() {
        requestHttp();

    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", contractUkid);

        NoHttpUtils.httpPost(ContractConstant.GET_ORDER_DETAILS, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        mOrderDetailsBean = JSON.parseObject(commonClass.getData().toString(), BuyOrderDetialBean.class);
                        if (mOrderDetailsBean != null) {
                            setData(mOrderDetailsBean);
                        }
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestHttp();
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showSystemView(result, true);
            mStateLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestHttp();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    private void setData(BuyOrderDetialBean orderDetailsBean) {
        if (mOrderType == 1) {
            mNeedName.setText("需方：" + orderDetailsBean.getDemanderName());
            mSupplierName.setText("供方: " + orderDetailsBean.getSupplierName());
            ImageloaderUtils.displayImg(ImageloaderUtils.getImageLoader(), orderDetailsBean.getSupplierLogoUrl(), mImgPhoto);
        } else if (mOrderType == 2) {
            mNeedName.setText("供方: " + orderDetailsBean.getSupplierName());
            mSupplierName.setText("需方：" + orderDetailsBean.getDemanderName());
            ImageloaderUtils.displayImg(ImageloaderUtils.getImageLoader(), orderDetailsBean.getDemanderLogoUrl(), mImgPhoto);
        }
        mType.setText("合计商品种类：" + orderDetailsBean.getSkuCount());
        mCount.setText("总计商品件数：" + orderDetailsBean.getItemCount());
        mOrderTime.setText("下单时间：" + orderDetailsBean.getContractTime());
        mOrderNumber.setText("订单号：" + orderDetailsBean.getContractId());

        mWeekPay.setText(Html.fromHtml("周期付金额：<font color='#337CFF'>" + orderDetailsBean.getPeriodPay() + "元</font>"));
        mOrderPay.setText(Html.fromHtml("下单付金额：<font color='#337CFF'>" + orderDetailsBean.getOrderPay() + "元</font>"));
        if (0 == orderDetailsBean.getBank()) {
            mBankCardPay.setVisibility(View.GONE);
        } else {
            mBankCardPay.setText("银行卡支付：" + orderDetailsBean.getBank() + "元");
        }
        if (0 == orderDetailsBean.getAliPay()) {
            mAliPay.setVisibility(View.GONE);
        } else {
            mAliPay.setText("支付宝支付：" + orderDetailsBean.getAliPay() + "元");
        }

        if (0 == orderDetailsBean.getWechatPay()) {
            mWechatPay.setVisibility(View.GONE);
        } else {
            mWechatPay.setText("微信支付：" + orderDetailsBean.getWechatPay() + "元");
        }

        if (0 == orderDetailsBean.getOffline()) {
            mOfflinePay.setVisibility(View.GONE);
        } else {
            mOfflinePay.setText("线下支付：" + orderDetailsBean.getOffline() + "元");
        }

        mTotalMoney.setText(Html.fromHtml("合计：<font color='#337CFF'>" + orderDetailsBean.getTotalPrice() + "元</font>"));
        mAddress.setText("配送地址：" + orderDetailsBean.getAddress());
        String contant = "";
        if (!TextUtils.isEmpty(orderDetailsBean.getReceiveName())) {
            contant = "联系人：" + orderDetailsBean.getReceiveName() + "\t\t";
        }
        if (!TextUtils.isEmpty(orderDetailsBean.getMobilePhone())) {
            contant += orderDetailsBean.getMobilePhone() + "\t";
        }

        mContant.setText(contant);

    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onUpPull() {
        if (isShow) {
            mBtnQuery.setVisibility(View.VISIBLE);
            mSpaceView.setVisibility(View.VISIBLE);
        } else {
            mBtnQuery.setVisibility(View.GONE);
            mSpaceView.setVisibility(View.INVISIBLE);
        }
        isShow = !isShow;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_query) {
            QueryExpressFragment queryExpressFragment = new QueryExpressFragment();
            Bundle bundle = new Bundle();
            bundle.putString("contractId", mOrderDetailsBean.getContractId());
            queryExpressFragment.setArguments(bundle);
            pushFragment(queryExpressFragment);
        } else if (i == R.id.btn_ordergoods) {
            BuyGoodDetailViewPagerFragment buyGoodDetailViewPagerFragment = new BuyGoodDetailViewPagerFragment();
            Bundle bundle = new Bundle();
            bundle.putString("contractUkid", contractUkid);
            buyGoodDetailViewPagerFragment.setArguments(bundle);
            pushFragment(buyGoodDetailViewPagerFragment);

        }
    }
}
